{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Importing PulP solver\n",
    "from pulp import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Inputing the demand points\n",
    "demand_points=['Mina Salman','Larnaca Port','Port of Alexandria','Port of Bandar Abbas','Port of Umm Qasr','Port of Haifa','Port of Aqaba','Port of Shuwaikh','Port of Beirut','Port of Salalah','Port of Gaza','Port of Doha','Port of King Abdul Aziz','Port of Jeddah','Port of Latakia','Port of Mersin','Port of Jebel Ali','Port of Hodeidah']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Inputing the Supply points\n",
    "supply_points=['Port of King Abdul Aziz','Port of Jeddah','Port of Mersin','Port of Jebel Ali']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Inputing the distances\n",
    "distance = dict(zip(supply_points,[dict(zip(demand_points, [82.9374,1330.7564,1475.7564,365,274,1150.7564,1205.184,203,1216.1991,257.8835,1233.7564,219,0,894.1688,1291.5533,1380.5533,389,821.4128])),\n",
    "dict(zip(demand_points, [918.7908,1035,1024,1259.1688,1063.6569,911,571,992.6569,981,1093.7369,828,1044.4928,894.1688,0,1076,1144,1231.102,539])),\n",
    "dict(zip(demand_points, [1410.3222,253.5098,546,1693.758,1112.758,233,595.0412,1177.5533,191,1585.2683,316,1550.3222,1380.5533,1144,89,0,1718.758,1591])),\n",
    "dict(zip(demand_points, [327,1684.3222,1829.3222,173,606,1504.3222,1560.0458,562,1566.5253,501.9461,1587.3222,187,389,1231.102,1629.758,1718.758,0,1180.4128]))]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Constraint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "vehicle_demands=dict(zip(demand_points, [52926740.45,20211709.62,219933980,150919247.3,216546853.3,219082653.4,107530347.6,133005844.6,49856593.12,150185708.1,11357941.91,55444187.63,255172735.5,255172735.5,30226871.78,133474473.2,494016476.8,16109213.06\n",
    "]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Decision Variables\n",
    "X = LpVariable.dicts('X%s%s', (supply_points,demand_points),\n",
    "                     cat = 'Binary',\n",
    "                     lowBound = 0,\n",
    "                     upBound = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Setting up the minimization problem\n",
    "\n",
    "prob=LpProblem ('P Median',LpMinimize)\n",
    "prob+= sum(sum(vehicle_demands[j]*distance[i][j]*X[i][j] for j in demand_points)for i in supply_points)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "P Median:\n",
       "MINIMIZE\n",
       "34043031212.919567*XPort_of_Jebel_AliLarnaca_Port + 17307044127.15*XPort_of_Jebel_AliMina_Salman + 402330112148.356*XPort_of_Jebel_AliPort_of_Alexandria + 167752267145.92007*XPort_of_Jebel_AliPort_of_Aqaba + 26109029782.9*XPort_of_Jebel_AliPort_of_Bandar_Abbas + 78101614494.28593*XPort_of_Jebel_AliPort_of_Beirut + 10368063086.810001*XPort_of_Jebel_AliPort_of_Doha + 18028713340.053402*XPort_of_Jebel_AliPort_of_Gaza + 329570899144.5255*XPort_of_Jebel_AliPort_of_Haifa + 19015521293.951168*XPort_of_Jebel_AliPort_of_Hodeidah + 314143665019.521*XPort_of_Jebel_AliPort_of_Jeddah + 99262194109.5*XPort_of_Jebel_AliPort_of_King_Abdul_Aziz + 49262486098.429245*XPort_of_Jebel_AliPort_of_Latakia + 229410318608.2856*XPort_of_Jebel_AliPort_of_Mersin + 75385130456.5334*XPort_of_Jebel_AliPort_of_Salalah + 74749284665.2*XPort_of_Jebel_AliPort_of_Shuwaikh + 131227393099.8*XPort_of_Jebel_AliPort_of_Umm_Qasr + 20919119456.7*XPort_of_JeddahLarnaca_Port + 48628602199.44786*XPort_of_JeddahMina_Salman + 225212395520*XPort_of_JeddahPort_of_Alexandria + 61399828479.6*XPort_of_JeddahPort_of_Aqaba + 190032807519.64426*XPort_of_JeddahPort_of_Bandar_Abbas + 48909317850.71999*XPort_of_JeddahPort_of_Beirut + 57911054781.38406*XPort_of_JeddahPort_of_Doha + 9404375901.48*XPort_of_JeddahPort_of_Gaza + 199584297247.4*XPort_of_JeddahPort_of_Haifa + 8682865839.34*XPort_of_JeddahPort_of_Hodeidah + 608184672621.4337*XPort_of_JeddahPort_of_Jebel_Ali + 228167498694.7524*XPort_of_JeddahPort_of_King_Abdul_Aziz + 32524114035.280003*XPort_of_JeddahPort_of_Latakia + 152694797340.80002*XPort_of_JeddahPort_of_Mersin + 164263650801.5989*XPort_of_JeddahPort_of_Salalah + 132029169382.51773*XPort_of_JeddahPort_of_Shuwaikh + 230331554685.83276*XPort_of_JeddahPort_of_Umm_Qasr + 26896861931.75657*XPort_of_King_Abdul_AzizLarnaca_Port + 4389606243.39783*XPort_of_King_Abdul_AzizMina_Salman + 324568978562.472*XPort_of_King_Abdul_AzizPort_of_Alexandria + 129593854441.95839*XPort_of_King_Abdul_AzizPort_of_Aqaba + 55085525264.50001*XPort_of_King_Abdul_AzizPort_of_Bandar_Abbas + 60635543681.61019*XPort_of_King_Abdul_AzizPort_of_Beirut + 12142277090.970001*XPort_of_King_Abdul_AzizPort_of_Doha + 14012933522.290724*XPort_of_King_Abdul_AzizPort_of_Gaza + 252110765529.03177*XPort_of_King_Abdul_AzizPort_of_Haifa + 13232313805.411167*XPort_of_King_Abdul_AzizPort_of_Hodeidah + 192172409475.2*XPort_of_King_Abdul_AzizPort_of_Jebel_Ali + 228167498694.7524*XPort_of_King_Abdul_AzizPort_of_Jeddah + 39039615996.13588*XPort_of_King_Abdul_AzizPort_of_Latakia + 184268624442.02158*XPort_of_King_Abdul_AzizPort_of_Mersin + 38730416054.80635*XPort_of_King_Abdul_AzizPort_of_Salalah + 27000186453.8*XPort_of_King_Abdul_AzizPort_of_Shuwaikh + 59333837804.200005*XPort_of_King_Abdul_AzizPort_of_Umm_Qasr + 5123866463.424276*XPort_of_MersinLarnaca_Port + 74643757030.273*XPort_of_MersinMina_Salman + 120083953080*XPort_of_MersinPort_of_Alexandria + 63984987072.32111*XPort_of_MersinPort_of_Aqaba + 255620682468.35342*XPort_of_MersinPort_of_Bandar_Abbas + 9522609285.92*XPort_of_MersinPort_of_Beirut + 85956354943.7544*XPort_of_MersinPort_of_Doha + 3589109643.56*XPort_of_MersinPort_of_Gaza + 51046258242.200005*XPort_of_MersinPort_of_Haifa + 25629757978.46*XPort_of_MersinPort_of_Hodeidah + 849094771631.8145*XPort_of_MersinPort_of_Jebel_Ali + 291917609412.0*XPort_of_MersinPort_of_Jeddah + 352279562064.5522*XPort_of_MersinPort_of_King_Abdul_Aziz + 2690191588.42*XPort_of_MersinPort_of_Latakia + 238084642163.98322*XPort_of_MersinPort_of_Salalah + 156621471228.01718*XPort_of_MersinPort_of_Shuwaikh + 240964243384.40143*XPort_of_MersinPort_of_Umm_Qasr + 0.0\n",
       "SUBJECT TO\n",
       "_C1: XPort_of_Jebel_AliPort_of_Jebel_Ali + XPort_of_JeddahPort_of_Jeddah\n",
       " + XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz\n",
       " + XPort_of_MersinPort_of_Mersin = 1\n",
       "\n",
       "_C2: XPort_of_Jebel_AliMina_Salman + XPort_of_JeddahMina_Salman\n",
       " + XPort_of_King_Abdul_AzizMina_Salman + XPort_of_MersinMina_Salman = 1\n",
       "\n",
       "_C3: XPort_of_Jebel_AliLarnaca_Port + XPort_of_JeddahLarnaca_Port\n",
       " + XPort_of_King_Abdul_AzizLarnaca_Port + XPort_of_MersinLarnaca_Port = 1\n",
       "\n",
       "_C4: XPort_of_Jebel_AliPort_of_Alexandria + XPort_of_JeddahPort_of_Alexandria\n",
       " + XPort_of_King_Abdul_AzizPort_of_Alexandria\n",
       " + XPort_of_MersinPort_of_Alexandria = 1\n",
       "\n",
       "_C5: XPort_of_Jebel_AliPort_of_Bandar_Abbas\n",
       " + XPort_of_JeddahPort_of_Bandar_Abbas\n",
       " + XPort_of_King_Abdul_AzizPort_of_Bandar_Abbas\n",
       " + XPort_of_MersinPort_of_Bandar_Abbas = 1\n",
       "\n",
       "_C6: XPort_of_Jebel_AliPort_of_Umm_Qasr + XPort_of_JeddahPort_of_Umm_Qasr\n",
       " + XPort_of_King_Abdul_AzizPort_of_Umm_Qasr + XPort_of_MersinPort_of_Umm_Qasr\n",
       " = 1\n",
       "\n",
       "_C7: XPort_of_Jebel_AliPort_of_Haifa + XPort_of_JeddahPort_of_Haifa\n",
       " + XPort_of_King_Abdul_AzizPort_of_Haifa + XPort_of_MersinPort_of_Haifa = 1\n",
       "\n",
       "_C8: XPort_of_Jebel_AliPort_of_Aqaba + XPort_of_JeddahPort_of_Aqaba\n",
       " + XPort_of_King_Abdul_AzizPort_of_Aqaba + XPort_of_MersinPort_of_Aqaba = 1\n",
       "\n",
       "_C9: XPort_of_Jebel_AliPort_of_Shuwaikh + XPort_of_JeddahPort_of_Shuwaikh\n",
       " + XPort_of_King_Abdul_AzizPort_of_Shuwaikh + XPort_of_MersinPort_of_Shuwaikh\n",
       " = 1\n",
       "\n",
       "_C10: XPort_of_Jebel_AliPort_of_Beirut + XPort_of_JeddahPort_of_Beirut\n",
       " + XPort_of_King_Abdul_AzizPort_of_Beirut + XPort_of_MersinPort_of_Beirut = 1\n",
       "\n",
       "_C11: XPort_of_Jebel_AliPort_of_Salalah + XPort_of_JeddahPort_of_Salalah\n",
       " + XPort_of_King_Abdul_AzizPort_of_Salalah + XPort_of_MersinPort_of_Salalah\n",
       " = 1\n",
       "\n",
       "_C12: XPort_of_Jebel_AliPort_of_Gaza + XPort_of_JeddahPort_of_Gaza\n",
       " + XPort_of_King_Abdul_AzizPort_of_Gaza + XPort_of_MersinPort_of_Gaza = 1\n",
       "\n",
       "_C13: XPort_of_Jebel_AliPort_of_Doha + XPort_of_JeddahPort_of_Doha\n",
       " + XPort_of_King_Abdul_AzizPort_of_Doha + XPort_of_MersinPort_of_Doha = 1\n",
       "\n",
       "_C14: XPort_of_Jebel_AliPort_of_King_Abdul_Aziz\n",
       " + XPort_of_JeddahPort_of_King_Abdul_Aziz\n",
       " + XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz\n",
       " + XPort_of_MersinPort_of_King_Abdul_Aziz = 1\n",
       "\n",
       "_C15: XPort_of_Jebel_AliPort_of_Jeddah + XPort_of_JeddahPort_of_Jeddah\n",
       " + XPort_of_King_Abdul_AzizPort_of_Jeddah + XPort_of_MersinPort_of_Jeddah = 1\n",
       "\n",
       "_C16: XPort_of_Jebel_AliPort_of_Latakia + XPort_of_JeddahPort_of_Latakia\n",
       " + XPort_of_King_Abdul_AzizPort_of_Latakia + XPort_of_MersinPort_of_Latakia\n",
       " = 1\n",
       "\n",
       "_C17: XPort_of_Jebel_AliPort_of_Mersin + XPort_of_JeddahPort_of_Mersin\n",
       " + XPort_of_King_Abdul_AzizPort_of_Mersin + XPort_of_MersinPort_of_Mersin = 1\n",
       "\n",
       "_C18: XPort_of_Jebel_AliPort_of_Jebel_Ali + XPort_of_JeddahPort_of_Jebel_Ali\n",
       " + XPort_of_King_Abdul_AzizPort_of_Jebel_Ali\n",
       " + XPort_of_MersinPort_of_Jebel_Ali = 1\n",
       "\n",
       "_C19: XPort_of_Jebel_AliPort_of_Hodeidah + XPort_of_JeddahPort_of_Hodeidah\n",
       " + XPort_of_King_Abdul_AzizPort_of_Hodeidah + XPort_of_MersinPort_of_Hodeidah\n",
       " = 1\n",
       "\n",
       "_C20: XPort_of_King_Abdul_AzizMina_Salman\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C21: XPort_of_JeddahMina_Salman - XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C22: XPort_of_MersinMina_Salman - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C23: XPort_of_Jebel_AliMina_Salman - XPort_of_Jebel_AliPort_of_Jebel_Ali <= 0\n",
       "\n",
       "_C24: XPort_of_King_Abdul_AzizLarnaca_Port\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C25: XPort_of_JeddahLarnaca_Port - XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C26: XPort_of_MersinLarnaca_Port - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C27: XPort_of_Jebel_AliLarnaca_Port - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " <= 0\n",
       "\n",
       "_C28: XPort_of_King_Abdul_AzizPort_of_Alexandria\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C29: XPort_of_JeddahPort_of_Alexandria - XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C30: XPort_of_MersinPort_of_Alexandria - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C31: XPort_of_Jebel_AliPort_of_Alexandria\n",
       " - XPort_of_Jebel_AliPort_of_Jebel_Ali <= 0\n",
       "\n",
       "_C32: XPort_of_King_Abdul_AzizPort_of_Bandar_Abbas\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C33: XPort_of_JeddahPort_of_Bandar_Abbas - XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C34: XPort_of_MersinPort_of_Bandar_Abbas - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C35: XPort_of_Jebel_AliPort_of_Bandar_Abbas\n",
       " - XPort_of_Jebel_AliPort_of_Jebel_Ali <= 0\n",
       "\n",
       "_C36: - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz\n",
       " + XPort_of_King_Abdul_AzizPort_of_Umm_Qasr <= 0\n",
       "\n",
       "_C37: - XPort_of_JeddahPort_of_Jeddah + XPort_of_JeddahPort_of_Umm_Qasr <= 0\n",
       "\n",
       "_C38: - XPort_of_MersinPort_of_Mersin + XPort_of_MersinPort_of_Umm_Qasr <= 0\n",
       "\n",
       "_C39: - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " + XPort_of_Jebel_AliPort_of_Umm_Qasr <= 0\n",
       "\n",
       "_C40: XPort_of_King_Abdul_AzizPort_of_Haifa\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C41: XPort_of_JeddahPort_of_Haifa - XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C42: XPort_of_MersinPort_of_Haifa - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C43: XPort_of_Jebel_AliPort_of_Haifa - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " <= 0\n",
       "\n",
       "_C44: XPort_of_King_Abdul_AzizPort_of_Aqaba\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C45: XPort_of_JeddahPort_of_Aqaba - XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C46: XPort_of_MersinPort_of_Aqaba - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C47: XPort_of_Jebel_AliPort_of_Aqaba - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " <= 0\n",
       "\n",
       "_C48: - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz\n",
       " + XPort_of_King_Abdul_AzizPort_of_Shuwaikh <= 0\n",
       "\n",
       "_C49: - XPort_of_JeddahPort_of_Jeddah + XPort_of_JeddahPort_of_Shuwaikh <= 0\n",
       "\n",
       "_C50: - XPort_of_MersinPort_of_Mersin + XPort_of_MersinPort_of_Shuwaikh <= 0\n",
       "\n",
       "_C51: - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " + XPort_of_Jebel_AliPort_of_Shuwaikh <= 0\n",
       "\n",
       "_C52: XPort_of_King_Abdul_AzizPort_of_Beirut\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C53: XPort_of_JeddahPort_of_Beirut - XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C54: XPort_of_MersinPort_of_Beirut - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C55: XPort_of_Jebel_AliPort_of_Beirut - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " <= 0\n",
       "\n",
       "_C56: - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz\n",
       " + XPort_of_King_Abdul_AzizPort_of_Salalah <= 0\n",
       "\n",
       "_C57: - XPort_of_JeddahPort_of_Jeddah + XPort_of_JeddahPort_of_Salalah <= 0\n",
       "\n",
       "_C58: - XPort_of_MersinPort_of_Mersin + XPort_of_MersinPort_of_Salalah <= 0\n",
       "\n",
       "_C59: - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " + XPort_of_Jebel_AliPort_of_Salalah <= 0\n",
       "\n",
       "_C60: XPort_of_King_Abdul_AzizPort_of_Gaza\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C61: XPort_of_JeddahPort_of_Gaza - XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C62: XPort_of_MersinPort_of_Gaza - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C63: XPort_of_Jebel_AliPort_of_Gaza - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " <= 0\n",
       "\n",
       "_C64: XPort_of_King_Abdul_AzizPort_of_Doha\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C65: XPort_of_JeddahPort_of_Doha - XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C66: XPort_of_MersinPort_of_Doha - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C67: XPort_of_Jebel_AliPort_of_Doha - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " <= 0\n",
       "\n",
       "_C68: 0 XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C69: - XPort_of_JeddahPort_of_Jeddah + XPort_of_JeddahPort_of_King_Abdul_Aziz\n",
       " <= 0\n",
       "\n",
       "_C70: XPort_of_MersinPort_of_King_Abdul_Aziz - XPort_of_MersinPort_of_Mersin\n",
       " <= 0\n",
       "\n",
       "_C71: - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " + XPort_of_Jebel_AliPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C72: XPort_of_King_Abdul_AzizPort_of_Jeddah\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C73: 0 XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C74: XPort_of_MersinPort_of_Jeddah - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C75: - XPort_of_Jebel_AliPort_of_Jebel_Ali + XPort_of_Jebel_AliPort_of_Jeddah\n",
       " <= 0\n",
       "\n",
       "_C76: - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz\n",
       " + XPort_of_King_Abdul_AzizPort_of_Latakia <= 0\n",
       "\n",
       "_C77: - XPort_of_JeddahPort_of_Jeddah + XPort_of_JeddahPort_of_Latakia <= 0\n",
       "\n",
       "_C78: XPort_of_MersinPort_of_Latakia - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C79: - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " + XPort_of_Jebel_AliPort_of_Latakia <= 0\n",
       "\n",
       "_C80: - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz\n",
       " + XPort_of_King_Abdul_AzizPort_of_Mersin <= 0\n",
       "\n",
       "_C81: - XPort_of_JeddahPort_of_Jeddah + XPort_of_JeddahPort_of_Mersin <= 0\n",
       "\n",
       "_C82: 0 XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C83: - XPort_of_Jebel_AliPort_of_Jebel_Ali + XPort_of_Jebel_AliPort_of_Mersin\n",
       " <= 0\n",
       "\n",
       "_C84: XPort_of_King_Abdul_AzizPort_of_Jebel_Ali\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C85: XPort_of_JeddahPort_of_Jebel_Ali - XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C86: XPort_of_MersinPort_of_Jebel_Ali - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C87: 0 XPort_of_Jebel_AliPort_of_Jebel_Ali <= 0\n",
       "\n",
       "_C88: XPort_of_King_Abdul_AzizPort_of_Hodeidah\n",
       " - XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 0\n",
       "\n",
       "_C89: XPort_of_JeddahPort_of_Hodeidah - XPort_of_JeddahPort_of_Jeddah <= 0\n",
       "\n",
       "_C90: XPort_of_MersinPort_of_Hodeidah - XPort_of_MersinPort_of_Mersin <= 0\n",
       "\n",
       "_C91: XPort_of_Jebel_AliPort_of_Hodeidah - XPort_of_Jebel_AliPort_of_Jebel_Ali\n",
       " <= 0\n",
       "\n",
       "VARIABLES\n",
       "0 <= XPort_of_Jebel_AliLarnaca_Port <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliMina_Salman <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Alexandria <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Aqaba <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Bandar_Abbas <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Beirut <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Doha <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Gaza <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Haifa <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Hodeidah <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Jebel_Ali <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Jeddah <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_King_Abdul_Aziz <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Latakia <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Mersin <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Salalah <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Shuwaikh <= 1 Integer\n",
       "0 <= XPort_of_Jebel_AliPort_of_Umm_Qasr <= 1 Integer\n",
       "0 <= XPort_of_JeddahLarnaca_Port <= 1 Integer\n",
       "0 <= XPort_of_JeddahMina_Salman <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Alexandria <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Aqaba <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Bandar_Abbas <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Beirut <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Doha <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Gaza <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Haifa <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Hodeidah <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Jebel_Ali <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Jeddah <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_King_Abdul_Aziz <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Latakia <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Mersin <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Salalah <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Shuwaikh <= 1 Integer\n",
       "0 <= XPort_of_JeddahPort_of_Umm_Qasr <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizLarnaca_Port <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizMina_Salman <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Alexandria <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Aqaba <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Bandar_Abbas <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Beirut <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Doha <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Gaza <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Haifa <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Hodeidah <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Jebel_Ali <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Jeddah <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Latakia <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Mersin <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Salalah <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Shuwaikh <= 1 Integer\n",
       "0 <= XPort_of_King_Abdul_AzizPort_of_Umm_Qasr <= 1 Integer\n",
       "0 <= XPort_of_MersinLarnaca_Port <= 1 Integer\n",
       "0 <= XPort_of_MersinMina_Salman <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Alexandria <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Aqaba <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Bandar_Abbas <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Beirut <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Doha <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Gaza <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Haifa <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Hodeidah <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Jebel_Ali <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Jeddah <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_King_Abdul_Aziz <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Latakia <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Mersin <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Salalah <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Shuwaikh <= 1 Integer\n",
       "0 <= XPort_of_MersinPort_of_Umm_Qasr <= 1 Integer"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Setting up constraints\n",
    "plant_numbers=1\n",
    "prob+= sum(X[i][i] for i in supply_points)==plant_numbers\n",
    "\n",
    "for j in demand_points:\n",
    "    prob+= sum(X[i][j] for i in supply_points)==1\n",
    "    \n",
    "for j in demand_points:\n",
    "    for i in supply_points:\n",
    "        prob+= X[i][j]<=X[i][i] \n",
    "        \n",
    "#for i in supply_points:\n",
    "#    prob+= sum(vehicle_demands[j]*X[i][j] for j in demand_points) <= 920430000\n",
    "        \n",
    "prob\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Saving the model to a lp file\n",
    "prob.writeLP(\"p-median.lp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status: Optimal\n",
      "Objective:  1661381248994.315\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'XPort_of_Jebel_AliLarnaca_Port': 0.0,\n",
       " 'XPort_of_Jebel_AliMina_Salman': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Alexandria': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Aqaba': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Bandar_Abbas': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Beirut': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Doha': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Gaza': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Haifa': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Hodeidah': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Jebel_Ali': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Jeddah': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_King_Abdul_Aziz': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Latakia': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Mersin': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Salalah': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Shuwaikh': 0.0,\n",
       " 'XPort_of_Jebel_AliPort_of_Umm_Qasr': 0.0,\n",
       " 'XPort_of_JeddahLarnaca_Port': 0.0,\n",
       " 'XPort_of_JeddahMina_Salman': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Alexandria': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Aqaba': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Bandar_Abbas': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Beirut': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Doha': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Gaza': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Haifa': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Hodeidah': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Jebel_Ali': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Jeddah': 0.0,\n",
       " 'XPort_of_JeddahPort_of_King_Abdul_Aziz': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Latakia': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Mersin': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Salalah': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Shuwaikh': 0.0,\n",
       " 'XPort_of_JeddahPort_of_Umm_Qasr': 0.0,\n",
       " 'XPort_of_King_Abdul_AzizLarnaca_Port': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizMina_Salman': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Alexandria': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Aqaba': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Bandar_Abbas': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Beirut': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Doha': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Gaza': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Haifa': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Hodeidah': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Jebel_Ali': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Jeddah': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_King_Abdul_Aziz': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Latakia': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Mersin': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Salalah': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Shuwaikh': 1.0,\n",
       " 'XPort_of_King_Abdul_AzizPort_of_Umm_Qasr': 1.0,\n",
       " 'XPort_of_MersinLarnaca_Port': 0.0,\n",
       " 'XPort_of_MersinMina_Salman': 0.0,\n",
       " 'XPort_of_MersinPort_of_Alexandria': 0.0,\n",
       " 'XPort_of_MersinPort_of_Aqaba': 0.0,\n",
       " 'XPort_of_MersinPort_of_Bandar_Abbas': 0.0,\n",
       " 'XPort_of_MersinPort_of_Beirut': 0.0,\n",
       " 'XPort_of_MersinPort_of_Doha': 0.0,\n",
       " 'XPort_of_MersinPort_of_Gaza': 0.0,\n",
       " 'XPort_of_MersinPort_of_Haifa': 0.0,\n",
       " 'XPort_of_MersinPort_of_Hodeidah': 0.0,\n",
       " 'XPort_of_MersinPort_of_Jebel_Ali': 0.0,\n",
       " 'XPort_of_MersinPort_of_Jeddah': 0.0,\n",
       " 'XPort_of_MersinPort_of_King_Abdul_Aziz': 0.0,\n",
       " 'XPort_of_MersinPort_of_Latakia': 0.0,\n",
       " 'XPort_of_MersinPort_of_Mersin': 0.0,\n",
       " 'XPort_of_MersinPort_of_Salalah': 0.0,\n",
       " 'XPort_of_MersinPort_of_Shuwaikh': 0.0,\n",
       " 'XPort_of_MersinPort_of_Umm_Qasr': 0.0}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Solving the model\n",
    "\n",
    "prob.solve()\n",
    "\n",
    "print(\"Status:\",LpStatus[prob.status]) \n",
    "print(\"Objective: \",value(prob.objective))\n",
    "varsdict = {}\n",
    "for v in prob.variables():\n",
    "    varsdict[v.name] = v.varValue\n",
    "varsdict "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
